/*
 * GWT-Ext Widget Library
 * Copyright 2007 - 2008, GWT-Ext LLC., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
 
package com.hod.roommap.client.model;

import com.google.gwt.gears.client.database.DatabaseException;
import com.google.gwt.user.client.Window;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.BooleanFieldDef;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.IntegerFieldDef;
import com.gwtext.client.data.MemoryProxy;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.PagingToolbar;
import com.gwtext.client.widgets.ToolTip;
import com.gwtext.client.widgets.form.Field;
import com.gwtext.client.widgets.form.NumberField;
import com.gwtext.client.widgets.form.event.FieldListenerAdapter;
import com.gwtext.client.widgets.grid.BaseColumnConfig;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtextux.client.data.PagingMemoryProxy;

public class RoomGrid extends AbstractGrid {		
    
	private static RecordDef recordDef = new RecordDef(
            new FieldDef[]{														
            		new IntegerFieldDef("idRoom"), 					 
            		new StringFieldDef("number"), 																		 
            		new StringFieldDef("state"), 																															 
            		new IntegerFieldDef("floor"), 																		 
            		new IntegerFieldDef("idRoomType"), 														 
            		new BooleanFieldDef("active"), 																		 
            		new BooleanFieldDef("sharedBath"), 								 
            }
    );

	

	public RoomGrid() {      
    }

    public RoomGrid(boolean small,GearsDataStore ds) {
    	this.ds = ds;  
    	columns = new BaseColumnConfig[]{
                //new ColumnConfig("idRoom","idRoom",0, true, null,"idRoom"),
                new ColumnConfig("floor","floor",0, true, null,"floor"),
                new ColumnConfig("number","number",0, true, null,"number"),
                //new ColumnConfig("state","state",0, true, null,"state"),
                //new ColumnConfig("idRoomType","idRoomType",0, true, null,"idRoomType"),
                //new ColumnConfig("active","active",0, true, null,"active"),
                //new ColumnConfig("sharedBath","sharedBath",0, true, null,"sharedBath"),
        };
    	
    	getData();

        columnModel = getColumnConfigs();
        setColumnModel(columnModel);

        setFrame(true);
        setStripeRows(true);
        setIconCls("grid-icon");
        createPopUp();
        createPager(); 

        
    }
    protected void getData(){
    	getData("");
    }
    
    protected void getData(String idRoomType){
    	PagingMemoryProxy proxy = null;
		try {
			GearsQuery q = this.ds.getNamedQuery(Room.FIND_BY_NUMBER);
			if (idRoomType.length()>0) q.setString(0, idRoomType);
			Room[] data = (Room[]) q.execute();
			Room rs = null;
			Object[][]data2 = new Object[data.length][];
			for (int i = 0; i < data.length; i++) {
                rs = data[i];	               
                data2[i] = new Object[]{
                        rs.getIdRoom(),
                        rs.getNumber(),
                        rs.getState(),
                        rs.getFloor(),
                        rs.getIdRoomType(),
                        rs.getActive(),
                        rs.getSharedBath(),
                     };
            }
                        
			proxy = new PagingMemoryProxy(data2);
			
			
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			Window.alert(e.getMessage());
		} 

        ArrayReader reader = new ArrayReader(recordDef);
        store = new Store(proxy, reader);
        store.load(0,numberRecords);
        setStore(store);
    }
}
